package com.caigou.modules.website.adpter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.modules.website.bean.WebsiteWithPic
import com.caigou.modules.website.databinding.ItemWebsiteWithPicBinding

/**
 * @author #Suyghur.
 * Created on 2021/09/17
 */
class WebsiteWithPicAdapter(private val context: Context, private val items: MutableList<WebsiteWithPic>) : BaseAdapter() {

    override fun getCount(): Int {
        return items.size
    }

    override fun getItem(position: Int): Any {
        return items[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val binding = if (convertView == null) {
            ItemWebsiteWithPicBinding.inflate(LayoutInflater.from(context))
        } else {
            convertView.tag as ItemWebsiteWithPicBinding
        }

        val view = convertView ?: binding.root
        view.tag = binding

        val item = items[position]
        ImageLoader.load(context, item.logo, binding.websiteIvIcon)
        binding.websiteTvDesc.text = item.title
        return view

    }
}